package ru.susu.algebra.partition.operation;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import ru.susu.algebra.operation.IOperation;
import ru.susu.algebra.pair.Pair;
import ru.susu.algebra.partition.Partition;

/**
 * @author akargapolov
 * @since: 11.03.2010
 */
public class Partition2PairsOperation implements IOperation<Pair<Integer, Integer>[]>
{
	public static IOperation<Pair<Integer, Integer>[]> INSTANCE = new Partition2PairsOperation();

	@Override
	public Pair<Integer, Integer>[] performOperation(Object... sources)
	{
		List<Pair<Integer, Integer>> result = new ArrayList<Pair<Integer, Integer>>();
		Partition partition = (Partition)sources[0];
		int index = 1;
		IOperation<Integer[]> op = new Partition2VertexOperation();
		for (Integer value : op.performOperation(partition))
		{
			if (value != 0)
			{
				result.add(new Pair<Integer, Integer>(index, value));
			}
			index++;
		}
		return result.toArray((Pair<Integer, Integer>[])Array.newInstance(Pair.class, result.size()));
	}

}
